<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="org.bson.Document" %>
<%@ page import="com.mongodb.client.*" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="org.bson.conversions.Bson" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.LinkedList" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>添加学生</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<%
    request.setCharacterEncoding("utf-8");
    String url = "mongodb://g08:abc%408@mongo.zhledu.cn:27017/g08";
    MongoClient client = MongoClients.create(url);
    MongoIterable<String> names = client.listDatabaseNames();
    MongoDatabase g08 = client.getDatabase("g08");
    MongoCollection<Document> collection = g08.getCollection("student");
    List<String> majorList = (List<String>) session.getAttribute("majorList");
    Document query = new Document();
    String sno = request.getParameter("sno");
    if (sno != null && sno != "") {
        query.append("sno", Pattern.compile(sno));
    } else {
        sno = "";
    }
    String name = request.getParameter("name");
    if (name != null && name != "") {
        query.append("name", Pattern.compile(name));
    } else {
        name = "";
    }
    String sex = request.getParameter("sex");
    if (sex != null && sex != "") {
        query.append("sex", sex);
    } else {
        sex = "";
    }
    String idCard = request.getParameter("idCard");
    if (idCard != null && idCard != "") {
        query.append("idCard", Pattern.compile(idCard));
    } else {
        idCard = "";
    }
    String major = request.getParameter("major");
    if (major != null && major != "") {
        query.append("major", major);
    } else {
        major = "";
    }
    final FindIterable<Document> list = collection.find(query).sort(new Document("sno", -1));
%>
<div class="container">
    <center>
        <h1>学生名单</h1>
    </center>
    <table class="table table-bordered" style="width: 100%">
        <thead>
        <th style="width: 10%">学号</th>
        <th style="width: 10%">姓名</th>
        <th style="width: 5%">年龄</th>
        <th style="width: 5%">性别</th>
        <th style="width: 10%">专业</th>
        <th style="width: 15%">身份证号</th>
        <th style="width: 10%">手机</th>
        <th>电子邮件</th>

        </thead>
        <%
            for (Document student : list) {
        %>
        <tr>
            <td><%=student.getString("sno")%>
            </td>
            <td><%=student.getString("name")%>
            </td>
            <td><%=student.getInteger("age")%>
            </td>
            <td><%=student.getString("sex")%>
            </td>
            <td><%=student.getString("major")%>
            </td>
            <td><%=student.getString("idCard")%>
            </td>
            <td>
                <%=student.getString("phone")%>

            </td>
            <td>
                <%=student.getString("email")%>
            </td>
        </tr>
        <%
            }
        %>
    </table>

</div>


<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>

<script>

</script>

<%
    client.close();
%>

<script>
    $(function () {
        window.print();
    });

</script>
</body>
</html>
